package com.albertyjz.juc;

import java.util.concurrent.*;

public class MyThreadPoolDemo {
    /*public static void main(String[] args) throws InterruptedException {
        //ExecutorService threadPool= Executors.newFixedThreadPool(5);
        ExecutorService threadPool= Executors.newSingleThreadExecutor();
        ExecutorService threadPool1=Executors.newCachedThreadPool();//动态可扩容的线程池,这个方法高并发的时候可以整特别多的线程出来，内存可能溢出
        try {
            for(int i=0;i<1000000000;i++){
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
                TimeUnit.MILLISECONDS.sleep(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

     */
    public static void main(String[] args) {
        ExecutorService threadPool1=new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors()+1,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        try {
            for(int i=0;i<180;i++){
                threadPool1.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t办理业务");
                });
                TimeUnit.MILLISECONDS.sleep(0);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            threadPool1.shutdown();
        }
    }
}
